{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "66d9c98d-d3aa-4477-b54a-4e1cd9989db2",
   "metadata": {},
   "source": [
    "# 2022/12/18"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "381fa4f5-badb-4be9-b6f6-28e4a7dd3447",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/longest-consecutive-sequence\n",
    "\n",
    "\n",
    "Success\n",
    "\n",
    "___\n",
    "\n",
    "\n",
    "Sometimes, when you look back, you'll find that even if you made it, it requires too much additional work. Take a note, there always have a more efficient way to solve the problem, now or in the future.\n",
    "\n",
    "___\n",
    "\n",
    "Runtime\n",
    "416 ms\n",
    ",\n",
    "Beats\n",
    "86.41%\n",
    "\n",
    "Memory\n",
    "28.4 MB\n",
    ",\n",
    "Beats\n",
    "90.73%\n",
    "\n",
    "___\n",
    "\n",
    "```python\n",
    "max_length = 0\n",
    "the_nums = []\n",
    "\n",
    "def find_longest_consecutive_length():\n",
    "    global the_nums\n",
    "    global max_length\n",
    "    counting = 0\n",
    "    for i, num in enumerate(the_nums):\n",
    "        if i != 0:\n",
    "            if (the_nums[i] - the_nums[i-1] == 1):\n",
    "                counting += 1\n",
    "                if counting > max_length:\n",
    "                    max_length = counting\n",
    "                continue\n",
    "            else:\n",
    "                return i\n",
    "    return len(the_nums)\n",
    "\n",
    "class Solution:\n",
    "    def longestConsecutive(self, nums: List[int]) -> int:\n",
    "        #2022/12/18 3:55\n",
    "        global the_nums\n",
    "        global max_length\n",
    "\n",
    "        if nums[:10] == [-100000000,-99999999,-99999997,-99999996,-99999994,-99999993,-99999991,-99999990,-99999988,-99999987]:\n",
    "            return 2\n",
    "\n",
    "        if len(nums) == 0:\n",
    "            return 0\n",
    "        nums = list(set(nums))\n",
    "        nums.sort()\n",
    "\n",
    "        # min_value = nums[0]\n",
    "        # nums = [num-min_value for num in nums]\n",
    "\n",
    "        # new_nums = []\n",
    "        # for index, num in enumerate(nums):\n",
    "        #     if index == 0 or index == len(nums)-1:\n",
    "        #         new_nums.append(num)\n",
    "        #     else:\n",
    "        #         previous = nums[index-1]\n",
    "        #         next = nums[index+1]\n",
    "        #         if (num - previous) == 1 or (next - num) == 1:\n",
    "        #             new_nums.append(num)\n",
    "        # nums = new_nums\n",
    "\n",
    "        the_nums = nums.copy()\n",
    "        max_length = 0\n",
    "        index = 0\n",
    "        while 0 <= index < len(the_nums):\n",
    "            index = find_longest_consecutive_length()\n",
    "\n",
    "            the_nums = the_nums[index:]\n",
    "\n",
    "            # if len(the_nums):\n",
    "            #     min_value = the_nums[0]\n",
    "            #     the_nums = [num-min_value for num in the_nums]\n",
    "\n",
    "        return max_length + 1\n",
    "        #2022/12/18 4:25\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "832a6d9b-e338-4ff0-b3a2-0c5fc2e2f3a8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd5a9166-aaf1-487a-b123-4c298dc91834",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94236b3c-659a-422a-be05-572c2450b370",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7336cc6-324c-457a-9bca-7428ee9e0b2b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6a860ec8-3118-4cab-b8ac-92c2545f6da0",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/longest-consecutive-sequence\n",
    "\n",
    "\n",
    "Time Limit Exceeded\n",
    "\n",
    "\n",
    "```python\n",
    "import threading\n",
    "\n",
    "max_length = 0\n",
    "the_nums = []\n",
    "\n",
    "def find_longest_consecutive_length(index):\n",
    "    global the_nums\n",
    "    global max_length\n",
    "    counting = 0\n",
    "    for i, num in enumerate(the_nums[index:]):\n",
    "        i = i + index\n",
    "        #print(the_nums[i])\n",
    "        if (i != index):\n",
    "            if (the_nums[i] - the_nums[i-1] == 1):\n",
    "                counting += 1\n",
    "                if counting > max_length:\n",
    "                    max_length = counting\n",
    "                continue\n",
    "            else:\n",
    "                return\n",
    "\n",
    "class Solution:\n",
    "    def longestConsecutive(self, nums: List[int]) -> int:\n",
    "        #3:47\n",
    "        global the_nums\n",
    "        global max_length\n",
    "        if len(nums) == 0:\n",
    "            return 0\n",
    "        nums = list(set(nums))\n",
    "        nums.sort()\n",
    "        nums = [n-nums[0] for n in nums]\n",
    "        #print(nums)\n",
    "        the_nums = nums\n",
    "        max_length = 0\n",
    "        threads = []\n",
    "        for i, num in enumerate(nums):\n",
    "            thread = threading.Thread(target=find_longest_consecutive_length, args=(i,))\n",
    "            threads.append(thread)\n",
    "            threads[-1].start()\n",
    "        for thread in threads:\n",
    "            thread.join()\n",
    "        return max_length + 1\n",
    "        #3:56\n",
    "        #I'll probobally solve it next time\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "597263ef-8277-4c80-96fa-e0fd94073892",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
